package surreal.triviamaster.gui;

import surreal.triviamaster.R;
import surreal.triviamaster.beans.Question;
import surreal.triviamaster.data.books1;
import surreal.triviamaster.data.history1;
import surreal.triviamaster.data.history2;
import surreal.triviamaster.data.math1;
import surreal.triviamaster.data.music1;
import surreal.triviamaster.data.tv1;
import surreal.triviamaster.data.tv2;
import surreal.triviamaster.database.LocalCache;
import surreal.triviamaster.utilities.TriviaMasterApplication;
import surreal.triviamaster.utilities.TriviaMasterService;
import surreal.triviamaster.utilities.Utilities;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class TriviaMasterActivity extends Activity{

	private boolean doOnce = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		
		//getting the configuration
		getResources().getConfiguration();
		//display picture if we are not in landscape mode
		if(getResources().getConfiguration().orientation!=Configuration.ORIENTATION_LANDSCAPE) {
			ImageView image = (ImageView) findViewById(R.id.galaxy_image);
        	image.setImageResource(R.drawable.galaxy);
		} else {
			ImageView image = (ImageView) findViewById(R.id.galaxy_image);
        	image.setVisibility(View.GONE);
		} 
		
		// handles changing orientation
		final Boolean doneOnce = (Boolean) getLastNonConfigurationInstance();
		// doneOnce returns nothing and there are no extras added(this means that
		// every time this app is directed to this activity a extra needs to be added)
		if (doneOnce == null && getIntent().getExtras() == null) {
			doOnce();
		}
		
	    //Choose a category
	    Button chooseCat = (Button) findViewById(R.id.chooseCategory);
	    chooseCat.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(TriviaMasterActivity.this, CategorySelector.class);
				startActivity(intent);
            }
        });
		
	    //All Categories
	    Button allCats = (Button) findViewById(R.id.allCategories);
	    allCats.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(Utilities.isNetworkAvailable(TriviaMasterActivity.this)) {
	            	// setting the quiz to have the given category
					LocalCache db = ((TriviaMasterApplication) getApplication()).getLocalCache();
					db.setCurrentQuizCategory(0);
	
					// getting the quiz questions from server
					Intent intent = new Intent(
							TriviaMasterApplication.ACTION_GET_QUESTIONS, null,
							TriviaMasterActivity.this,
							TriviaMasterService.class);
					startService(intent);
	
					// starting the quiz activity
					//intent = new Intent(TriviaMasterActivity.this, Game.class);
					intent = new Intent(TriviaMasterActivity.this, LoadingScreen.class);
					startActivity(intent);
            	} else {
            		Toast.makeText(TriviaMasterActivity.this, TriviaMasterApplication.NETWORK_ERROR, 5000);
            	}
            }
        });
	    
	    //add question button
	    Button addQuestion = (Button) findViewById(R.id.addQuestion);
	    addQuestion.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) {
            	Intent intent = new Intent(TriviaMasterActivity.this, AddQuestion.class);
            	Bundle bundle = new Bundle();
            	bundle.putString("question", "");
            	bundle.putString("aAs", "");
            	intent.putExtra("bundle", bundle);
				startActivity(intent);
            }
        });
		
	    //test button
	    Button test = (Button) findViewById(R.id.test);
	    test.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Toast.makeText(TriviaMasterActivity.this, "Start Service",
						5000).show();
				 emptyQuestions();
				
				for (int j = 1; j < 2; j++) {
					Question[] qList;
					try {
						qList = books1.getQuestions();
						insertQuestions(qList);
						qList = history1.getQuestions();
						insertQuestions(qList);
						qList = history2.getQuestions();
						insertQuestions(qList);
						qList = math1.getQuestions();
						insertQuestions(qList);
						qList = music1.getQuestions();
						insertQuestions(qList);
						qList = tv1.getQuestions();
						insertQuestions(qList);
						qList = tv2.getQuestions();
						insertQuestions(qList);
					} catch (Exception e) {
						System.out.println(e.getMessage());
						e.printStackTrace();
					}
				}
				  
            }
        });

	}
	
	public void insertQuestions(Question[] qList) {
		for (int i = 0; i < qList.length; i++) {
			Intent intent = new Intent(
					TriviaMasterApplication.ACTION_INSERT_QUESTION, null,
					TriviaMasterActivity.this, TriviaMasterService.class);
			intent.putExtra("question", qList[i].getQuestion());
			intent.putExtra("acceptableAnswers", Utilities.analyzeaAs(qList[i].getAcceptableAnswersSplit()));
			intent.putExtra("hint", qList[i].getHint());
			intent.putExtra("fList", qList[i].getFalseAnswersSplitId());
			
			Log.d("insertQuestions", qList[i].getAcceptableAnswersSplit());
			Log.d("insertQuestions", Utilities.analyzeaAs(qList[i].getAcceptableAnswersSplit()));
			
			intent.putExtra("categories", qList[i].getCategoriesSplit());
			intent.putExtra("userId", "-1");

			startService(intent);
		}
	}

	@Override
	public Object onRetainNonConfigurationInstance() {
		return new Boolean(doOnce);
	}

	private void doOnce() {
		if(Utilities.isNetworkAvailable(TriviaMasterActivity.this)) {
        	doOnce = true;
		
			// Authenticating user
			Intent intent = new Intent(TriviaMasterApplication.ACTION_AUTHENTICATE, null,
					TriviaMasterActivity.this, TriviaMasterService.class);
			startService(intent);
					
			// reloading categories
			intent = new Intent(
					TriviaMasterApplication.ACTION_REFRESH_CATEGORIES, null,
					TriviaMasterActivity.this, TriviaMasterService.class);
			startService(intent);
    	} else {
    		Toast.makeText(TriviaMasterActivity.this, TriviaMasterApplication.NETWORK_ERROR, 5000);
    	}		
	}

	private void emptyQuestions() {
		if(Utilities.isNetworkAvailable(TriviaMasterActivity.this)) {
        	Intent intent = new Intent(
				TriviaMasterApplication.ACTION_EMPTY_QUESTIONS, null,
				TriviaMasterActivity.this, TriviaMasterService.class);
			intent.putExtra("code", "tHeMaStErCoDe");
			startService(intent);
    	} else {
    		Toast.makeText(TriviaMasterActivity.this, TriviaMasterApplication.NETWORK_ERROR, 5000);
    	}
	}
	
	@Override
	public void onBackPressed() {
	   return;
	}
}